<? 
	include('base.php');
	User::protect();
	include_class('tours');
	include_class('shows');
	
	$section = 'tours';
	$tour = Tour::get($_GET['id']);
	
	if (!db::isError($tour)) {
		switch($_GET['task']) {
			case 'update':
				$res = $tour->update($_POST);
				if (!db::isError($res)) {
					header('Location: tour_edit.php?id=' . $_GET['id']);
				}
				break;
			case 'deactivate':
				$res = $tour->deactivate();
				if (!db::isError($res)) {
					header('Location: tour_edit.php?id=' . $_GET['id']);
				}
				break;
			case 'activate':
				$res = $tour->activate();
				if (!db::isError($res)) {
					header('Location: tour_edit.php?id=' . $_GET['id']);
				}
				break;
			case 'delete':
				$res = $tour->remove();
				if (!db::isError($res)) {
					header('Location: tours.php');
				}
				break;
		}
	}
	if ($_GET['task'] == 'edit') {
		$calendar = true;
	}
	$editors = array('description');
	$page_title = 'Edit Tour';
	include('layout/header.php');
?>

<div id="breadcrumb">
	<a href="index.php">Audition &#62;</a>
	<a href="live.php">Live &#62;</a>
	<a href="tours.php">Tours &#62;</a>
	View/Edit Tour
</div>

<? 	if (!db::isError($tour)) { 

	if (db::isError($res)) {
		$res->outputList();
	}
	
	
	if (User::isAdmin() && ($_GET['task'] == 'edit' || $_GET['task'] == 'update')) {
	
		$start_date = $_POST ? $_POST['start_date'] : $tour->getStartDate();
		$end_date = $_POST ? $_POST['end_date'] : $tour->getEndDate();
		$title = $_POST ? $_POST['title'] : $tour->getTitle();
		$description = $_POST ? $_POST['description'] : $tour->getDescription();
	
		?>
		
		<h1>edit tour:</h1>
		<div class="inset">
		<form id="edit_entry" action="<?=$PHP_SELF?>?task=update&amp;id=<?=$_GET['id']?>" method="post">
		
		<div class="inset">
		<form id="edit_entry" action="<?=$PHP_SELF?>" method="post">
		<table border="0" class="edit-form" cellspacing="0" cellpadding="0">
		<? if (!$tour->isActive()) { ?>
		<tr>	
			<td colspan="2" class="fieldname">This tour is inactive.</td>
		</tr>
		<? } ?>
		<tr>
			<td width="50%"><strong>Start Date (<a style="vertical-align: middle" href="javascript:void(0)" onclick="popUpCalendar(this, document.getElementById('start_date'), '<?=DATE_JS_CALENDAR?>')">Calendar</a>)</strong><br/>
			<input type="text" name="start_date" class="text" id="start_date" value="<?=$start_date?>"></td>
			<td width="50%"><strong>End Date (<a style="vertical-align: middle" href="javascript:void(0)" onclick="popUpCalendar(this, document.getElementById('end_date'), '<?=DATE_JS_CALENDAR?>')">Calendar</a>)</strong><br/>
			<input type="text" name="end_date" class="text" id="end_date" value="<?=$end_date?>"></td>
		</tr>
		</table>
		<div class="edit-form">
			<strong>Title</strong><br/>
			<input type="text" name="title" class="text" value="<?=$title?>">
		</div>
		<div class="edit-form">
		<strong>Description:</strong><br/>
		<textarea name="description" id="description" class="full"><?=$description?></textarea>
		</div>	
		
		<div class="edit-form">
			<input type="submit" value="Update Tour" class="button" name="submit">
			<input type="submit" value="Exit Without Updating" class="button" name="preview" onclick="location.href='<?=$PHP_SELF?>?id=<?=$tour->getID()?>'; return false">
		</div>
		</div>
		
	<? } else { ?>

		<h1>view tour:</h1>
		<div class="inset">
		
		<h2><?=$tour->getTitle()?></h2>
		
		<span class="small">
			Status: <strong><? if ($tour->isActive()) { ?>Active<? } else { ?>Inactive<? } ?></strong>
		</span>
		
		<br><br>

		<strong>Tour Length</strong><br>
		<?=$tour->getStartDate()?> to <?=$tour->getEndDate()?>

		<br><br>
		
		<strong>Description</strong><br>
			
			<? if ($tour->getDescription()) { ?>
				<?=$tour->getDescription()?>
			<? } else { ?>
				None
			<? } ?>
		
		<br><br>
		<strong>Shows in this Tour</strong><br>
		<?
			$shows = $tour->getShows();
			if (count($shows) > 0) { ?>
			
			<table border="0" cellspacing="1" cellpadding="0" class="list">
			<?
				foreach ($shows as $sh) {
					echo('<tr><td>');
					echo($sh->getDate());
					echo('</td><td>');
					echo("<a href=\"show_edit.php?id=" . $sh->getID() . "\"><strong>" . $sh->getName() . "</strong></a>");
					echo('</td></tr>');					
				} 
			?>
			</table>	
			
			<?
			} else {
				echo("There are no shows in this tour.");
			}
		?>
		</div>
		<form name="entry_status" method="get">
			<? if (User::isAdmin()) { ?>
				<input type="submit" name="" value="Edit Tour" onclick="location.href='<?=$PHP_SELF?>?task=edit&id=<?=$_GET['id']?>';return false" class="button">
			<? } ?>
			
			<input type="button" name="" value="Photos" onclick="location.href='tour_media.php?id=<?=$_GET['id']?>&type=photos';return false" class="button">
			<input type="button" name="" value="Audio/Video" onclick="location.href='tour_media.php?id=<?=$_GET['id']?>&type=av';return false" class="button">

			<? if (User::isAdmin()) { ?>
				<? if ($tour->isActive()) { ?><input type="submit" name="" value="Deactivate" onclick="location.href='<?=$PHP_SELF?>?task=deactivate&id=<?=$_GET['id']?>';return false" class="button"><? } else { ?><input type="submit" name="" value="Activate" onclick="location.href='<?=$PHP_SELF?>?task=activate&id=<?=$_GET['id']?>';return false" class="button"><? } ?>
				<input type="submit" value="Remove Tour" name="" onclick="if (confirm('Are you sure you wish to delete this tour?\rWhenever possible, deactivate rather than delete.')) { location.href='<?=$PHP_SELF?>?task=delete&id=<?=$_GET['id']?>'};return false" class="button">
			<? } ?>
			<input type="submit" class="button" value="Return to Tour List" name="" onclick="location.href='tours.php'; return false">
		</form>
		
		</div>
		
		<? } ?>

<? } else {
	$tour->outputError('Return to Tours','tours.php');
} ?>

<? include('layout/footer.php'); ?>
